package com.yq.servlet;

import com.yq.bean.Product;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.ArrayList;

@WebListener
public class ProductServletContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        //servletContext初始化之前做的
        ArrayList<Product> list = new ArrayList<>();
        Product product1 = new Product("1","商品1","河南");
        Product product2 = new Product("2","商品2","安徽");
        Product product3 = new Product("3","商品3","武汉");
        Product product4 = new Product("4","商品4","湖北");
        Product product5 = new Product("5","商品5","北京");
        list.add(product1);
        list.add(product2);
        list.add(product3);
        list.add(product4);
        list.add(product5);
        ServletContext servletContext = servletContextEvent.getServletContext();
        servletContext.setAttribute("products",list);

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        //servletContext结束


    }
}
